﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ALinq.Access;
using ALinq.Mapping;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NorthwindDemo;

namespace TestProject
{
    [TestClass]
    public class ModifyMappingTest
    {
        class NorthwindMappingSource : FluentMappingSource
        {
            public NorthwindMappingSource(Action<DatabaseMapping<NorthwindDatabase>> func = null)
            {
                Map<NorthwindDatabase>(mapping =>
                {
                    mapping.ProviderType = typeof(AccessDbProvider);
                    mapping.Table(o => o.Categories, "Categories")
                            .PrimaryKey(o => o.CategoryID)
                            .Column(o => o.CategoryName)
                            .Column(o => o.Description);

                    if (func != null)
                        func(mapping);
                });
            }
        }

        [TestMethod]
        public void AppendColumn()
        {
            var ms = new NorthwindMappingSource();
            var columnsCount1 = ms.GetModel(typeof(NorthwindDatabase)).GetTable(typeof(Category)).RowType.DataMembers.Count;

            var mappingSource = new NorthwindMappingSource(mapping =>
            {
                mapping.Table(o => o.Categories, "Categories")
                        .Column(o => o.Products);
            });

            var columnsCount2 = mappingSource.GetModel(typeof(NorthwindDatabase)).GetTable(typeof(Category)).RowType.DataMembers.Count;
            Assert.AreEqual(columnsCount1 + 1, columnsCount2);
        }

        [TestMethod]
        public void AppendTable()
        {
            var ms = new NorthwindMappingSource();
            var tablesCount1 = ms.GetModel(typeof(NorthwindDatabase)).GetTables().Count();

            var mappingSource = new NorthwindMappingSource(mapping =>
            {
                mapping.Table(o => o.Products, "Products")
                        .PrimaryKey(o => o.ProductID)
                        .Column(o => o.ProductName);
            });

            var tablesCount2 = mappingSource.GetModel(typeof(NorthwindDatabase)).GetTables().Count();
            Assert.AreEqual(tablesCount1 + 1, tablesCount2);
        }

        [TestMethod]
        public void RemoveColumn()
        {
            var ms = new NorthwindMappingSource();
            var columnsCount1 = ms.GetModel(typeof(NorthwindDatabase)).GetTable(typeof(Category)).RowType.DataMembers.Count;

            var mappingSource = new NorthwindMappingSource(mapping =>
            {
                var table = mapping.GetTable(typeof(Category));
                var column = table.RowType.GetColumn(typeof(Category).GetProperty("CategoryName"));
                table.RowType.Columns.Remove(column);
            });

            var columnsCount2 = mappingSource.GetModel(typeof(NorthwindDatabase)).GetTable(typeof(Category)).RowType.DataMembers.Count;
            Assert.AreEqual(columnsCount1 - 1, columnsCount2);
        }

        [TestMethod]
        public void RemoveTable()
        {
            var ms = new NorthwindMappingSource();
            var tablesCount1 = ms.GetModel(typeof(NorthwindDatabase)).GetTables().Count();

            var mappingSource = new NorthwindMappingSource(mapping =>
            {
                var table = mapping.GetTable(typeof(Category));
                mapping.Tables.Remove(table);
            });

            var tablesCount2 = mappingSource.GetModel(typeof(NorthwindDatabase)).GetTables().Count();
            Assert.AreEqual(tablesCount1 - 1, tablesCount2);
        }
    }
}
